//
//  MineViewController.m
//  FNer
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "MineViewController.h"

@interface MineViewController ()
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,assign)CGFloat myFloat;
@property(nonatomic,retain)UIImageView *imageView;
@end

@implementation MineViewController

{
    BOOL isNight;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent=YES;
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
    self.navigationItem.title=@"我的";
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myFloat=0.5;
    //相对于0点,图片坐标应该是(0,-200)
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, self.view.frame.size.width,200)];
    self.imageView.image =[[UIImage imageNamed:@"beijing1"]boxblurImageWithBlur:self.myFloat];
    //设置imageView高度改变时宽度也跟着改变
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;

    [self.myTableView addSubview:self.imageView];
 
    
    
    


}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   
    return 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 40*HIGHT)];
    v.backgroundColor = [UIColor colorWithWhite:0.144 alpha:0.9];
    return v;
}




//指定多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

//创建一个cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cell";
    UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    //     cell.selectionStyle =  UITableViewCellSelectionStyleBlue;
    if (indexPath.section==0 && indexPath.row==0) {
        cell.textLabel.text = @"清除缓存";
        
    }else if (indexPath.section==0 && indexPath.row==1) {
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"isNight"]isEqualToString:@"YES"]) {
              cell.textLabel.text = @"日间模式";
            SHAREDAPPDELE.window.alpha = 0.5;
        }
        else
        {
            cell.textLabel.text = @"夜间模式";
            SHAREDAPPDELE.window.alpha = 1.0;
        }
        
        
      
        
    }else if (indexPath.row==0){
        cell.textLabel.text = @"联系我们";
        
    }else if (indexPath.row==1){
        cell.textLabel.text = @"测试";
        
    }
    
    self.myTableView.tableFooterView = [[UIView alloc] init];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    
    if (y>-(364)&&y<-(264)) {
        self.myFloat=(y+364)/200;
        NSLog(@"%f",self.myFloat);
        self.imageView.image =[[UIImage imageNamed:@"beijing1"] boxblurImageWithBlur:self.myFloat];
    }
    
    
    if (y < -263) {
        
        CGRect frame = self.imageView.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
        self.imageView.frame = frame;
    }
    if (scrollView == self.myTableView)
    {
        CGFloat sectionHeaderHeight = 40;//40自己定义的section的headerview的高度
    NSLog(@"%f",scrollView.contentOffset.y)
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=-264) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }

    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //点击后自动取消选中置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
   
    if (indexPath.section==0) {
        
        switch (indexPath.row) {
            case 0:{
                NSUInteger size = [[SDImageCache sharedImageCache]getSize];
                CGFloat cache = size / 1024. / 1024.;
                NSString *strCa = [NSString stringWithFormat:@"%.2fMB", cache];
                NSString *str = [NSString stringWithFormat:@"一共有%ld个文件可以清除, 约%@", [self numberOfFile], strCa];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清除缓存" message:str delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"清除", nil];
                [alert show];
            }break;
            case 1:{
                if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"isNight"] isEqualToString:@"YES"]) {
                    SHAREDAPPDELE.window.alpha = 1;
                    [[NSUserDefaults standardUserDefaults]setValue:@"NO" forKey:@"isNight"];
                   UITableViewCell *cell =   [self.myTableView cellForRowAtIndexPath:indexPath];
                    cell.textLabel.text = @"夜间模式";
                }
                else
                {
                   SHAREDAPPDELE.window.alpha = 0.5;
                    [[NSUserDefaults standardUserDefaults]setValue:@"YES" forKey:@"isNight"];
                    UITableViewCell *cell =   [self.myTableView cellForRowAtIndexPath:indexPath];
                    cell.textLabel.text = @"日间模式";
       
                }
            }break;
                default:break;
        }

    }else
    {
        switch (indexPath.row) {
            case 0:{
              
                
                
                
                
                
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@" FNer " message:@"版本：1.0" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil , nil];
                [alert show];
            }break;
            case 1:{
                NSLog(@"测试");
            }break;
            default:break;
        }

    }
    
}
- (NSInteger)numberOfFile{
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSArray *files = [[NSFileManager defaultManager]subpathsAtPath:cachPath];
    return [files count];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
